package jacker.bit.price;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class ActivityPlatforms extends Activity {
	
	ListView smtplist;
    List<Map<String,Object>> platforms = new ArrayList<Map<String,Object>>();
    
    SimpleAdapter adp1;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.platforms);
        smtplist = (ListView)findViewById(R.id.smtplist);
        
        adp1 = new SimpleAdapter(this,platforms,R.layout.serveritem,
           		new String[]{"selected"},new int[]{R.id.selected}){
      	  @Override
    	  public View getView(final int position, View convertView, ViewGroup parent) {
    		  
    		  View view = super.getView(position, convertView, parent);
    		  
    		  CheckBox rb = (CheckBox)view.findViewById(R.id.selected);
    		  rb.setText((String)platforms.get(position).get("content"));
    		  
    		  rb.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					CheckBox rb0 = (CheckBox)v;
					Map<String,Object> sitem = platforms.get(position);
					String threadid = (String)sitem.get("threadid");
					String threadname = (String)sitem.get("content");
					String currency = (String)sitem.get("currency");
					PriceHelper mh = new PriceHelper(ActivityPlatforms.this);
					if(rb0.isChecked()){
						CurrentPrice cp = mh.getCurrentPrice(threadid);
						if(cp==null){
							cp = new CurrentPrice();
							cp.setThreadid(threadid);
							cp.setThreadname(threadname);
							cp.setCurrency(currency);
							mh.insertCurrentPrice(cp);
						}
						
						sitem.put("selected",true);
							
					}else{
						mh.deleteCurrentPrice(threadid);
						sitem.put("selected",false);
					}
					
					PrefStore.configTime = System.currentTimeMillis();
				}
    			  
    		  });
    		  
    		  
    		  return view;
    	  }
    };
       smtplist.setAdapter(adp1);
        
       getSmtps();
       
    }
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(Menu.NONE,5,5,R.string.back_action)
			.setIcon(android.R.drawable.ic_menu_revert);
		
		return true;
	}
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId()==5){
			finish();
			return true;
		}
		return false;
    }

    
    public void getSmtps(){
    	List<Platform> all =  PriceChecker.getPlatforms();
		//emails.clear();
		platforms.clear();
		
		PriceHelper ph = new PriceHelper(this);

		for(Platform smtp:all){
			Map<String,Object> item = new HashMap<String,Object>();
			item.put("threadid", smtp.getCode());
			item.put("content", smtp.getName(this));
			item.put("currency", smtp.getCurrency());
			item.put("selected", ph.getCurrentPrice(smtp.getCode())!=null);
			platforms.add(item);
				
		}
		
		//adp0.notifyDataSetChanged();
        adp1.notifyDataSetChanged();
		
	}
    
    
}